<?php

/**
 * Date time API
 *
 * @author trung - KeiMOon
 * @package OWL
 * @subpackage Helper
 */
class OWL_Date_Time {

    /**
     * Retrieve the current time based on specified type.
     *
     * The 'xml' type will return the time in the format for W3C Datetime field.
     * The 'timestamp' type will return the current timestamp.
     *
     * If $gmt is set to either '1' or 'true', then both types will use GMT time.
     * if $gmt is false, the output is adjusted with the GMT offset in the Owl option.
     *
     * @since 1.0.0
     *
     * @param string $type Either 'xml' or 'timestamp'.
     * @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false.
     * @return int|string String if $type is 'gmt', int if $type is 'timestamp'.
     */
    static function current_time( $type = 'xml', $gmt = 0 ) {
        switch ( $type ) {
            case 'mysql':
                return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option("gmt_offset", DEFAULT_GMT_OFFSET) * 3600 ) ) );
                break;
            case 'xml':
                return ( $gmt ) ? gmdate( DATE_W3C ) : gmdate( DATE_W3C, ( time() + ( get_option("gmt_offset", DEFAULT_GMT_OFFSET) * 3600 ) ) );
                break;
            case 'timestamp':
                return ( $gmt ) ? time() : time() + ( get_option("gmt_offset", DEFAULT_GMT_OFFSET) * 3600 );
                break;
        }
    }

    static function timestamp_to_xml($timestamp) {
        return gmdate(DATE_W3C, $timestamp);
    }

    /**
     * Get the current year
     *
     * @return string current year
     */
    static function get_current_year() {
        return date("Y", time());
    }

    /**
     * Retrieve the date in localized format, based on timestamp.
     *
     * If the locale specifies the locale month and weekday, then the locale will
     * take over the format for the date. If it isn't, then the date format string
     * will be used instead.
     *
     * @since 0.71
     *
     * @param string $dateformatstring Format to display the date.
     * @param int $unixtimestamp Optional. Unix timestamp.
     * @param bool $gmt Optional, default is false. Whether to convert to GMT for time.
     * @return string The date, translated if locale specifies it.
     */
    static function date_i18n($dateformatstring, $unixtimestamp = false, $gmt = false) {
        global $locale;
	$i = $unixtimestamp;
	// Sanity check for PHP 5.1.0-
	if ( false === $i || intval($i) < 0 ) {
		if ( ! $gmt )
			$i = self::current_time( 'timestamp' );
		else
			$i = time();
		// we should not let date() interfere with our
		// specially computed timestamp
		$gmt = true;
	}

	// store original value for language with untypical grammars
	// see http://core.trac.wordpress.org/ticket/9396
	$req_format = $dateformatstring;

	$datefunc = $gmt? 'gmdate' : 'date';

	if ( ( !empty( $locale->month ) ) && ( !empty( $locale->weekday ) ) ) {
		$datemonth = $locale->get_month( $datefunc( 'm', $i ) );
		$datemonth_abbrev = $locale->get_month_abbrev( $datemonth );
		$dateweekday = $locale->get_weekday( $datefunc( 'w', $i ) );
		$dateweekday_abbrev = $locale->get_weekday_abbrev( $dateweekday );
		$datemeridiem = $locale->get_meridiem( $datefunc( 'a', $i ) );
		$datemeridiem_capital = $locale->get_meridiem( $datefunc( 'A', $i ) );
		$dateformatstring = ' '.$dateformatstring;
		$dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . Owl_Format::backslashit( $dateweekday_abbrev ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . Owl_Format::backslashit( $datemonth ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . Owl_Format::backslashit( $dateweekday ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . Owl_Format::backslashit( $datemonth_abbrev ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . Owl_Format::backslashit( $datemeridiem ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . Owl_Format::backslashit( $datemeridiem_capital ), $dateformatstring );

		$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
	}
	$j = @$datefunc( $dateformatstring, $i );

	return $j;
    }

    /**
     * Convert W3C date time string to user defined date format, with user's gmt offset
     *
     * @param string $string
     */
    static function w3c_to_date($string, $date_format, $gmt = true, $locale = true) {
        if ($gmt)
            $timestamp = strtotime($string) + get_option("gmt_offset", DEFAULT_GMT_OFFSET) * 3600;
        else
            $timestamp = strtotime($string);
        if ($locale)
            return self::date_i18n($date_format, $timestamp, true);
        else
            return gmdate($date_format, $timestamp);
    }

    /**
     * Convert W3C date to OWL date
     *
     * @param string $string
     * @param string $type long or shirt
     * @return string 
     */
    static function w3c_to_owl_date($string, $type = 'long') {
        if ($type == 'long') $date_format = "l, d F Y, H:i:s";
        else $date_format = "d/m/Y H:i:s";
        return self::w3c_to_date($string, $date_format);
    }
}

